package com.none.web.controller.book;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TBookinfo;
import com.none.web.service.book.IBookService;

/**
 * book管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("book")
public class BookController {

	@Resource(name = "bookService")
	IBookService bookService;
	@Autowired
	private HttpServletRequest request;
	
	
	/**
	 * 管理后台 书籍分类list
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("searchBookcategorys")
	public Object searchBookcategorys() throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("bookcategorys", bookService.searchBookcategorys());
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.havaBook", data);
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 新增书籍
	 * @param bookInfo
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveOrUpdateBook")
	public Object saveOrUpdateBook(@FormModel("bookInfo") final TBookinfo bookInfo,Long publishertime) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.saveSuccess", data);
		if (!bookService.saveOrUpdateBook(bookInfo,publishertime)) {
			result = new Result(SysConstant.STATE_ERROR, "books.saveError", data);
		}
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 删除书籍
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("delBookinfo")
	public Object delBookinfo(Integer id) throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.delSuccess", data);
		if (!bookService.delBookinfo(id)) {
			result = new Result(SysConstant.STATE_ERROR, "books.delError", data);
		}
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 查看书籍列表
	 * @param pager 分页
	 * @param keyword 关键字
	 * @param cateId 类型编号
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("searchBooks")
	public Object searchBooks(@FormModel("pager") final Pager pager,String keyword,Integer cateId,String fromDate,
			String toDate) throws Exception{
		String[] filter = new String[] { "book_catalog", "catalog_id", "book_desp", "book_Author", "book_publisher_year","book_year","book_vol", "book_open", "book_cover","book_isbn","addtime"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("books", bookService.searchBooks(pager, keyword, cateId, fromDate, toDate));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.havaBook", data);
		return JSONUtil.toJSON(result,filter);
	}
	
	/**
	 * 根据书籍ID查找书籍
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("searchBookById")
	public Object searchBookById(Integer id){
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("book", bookService.searchBookById(id));
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.havaBook", data);
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 查看预约记录
	 * @param pager
	 * @param detailstatus 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("searchBookstats")
	public Object searchBookstats(@FormModel("pager") final Pager pager,Integer code) throws Exception{
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("bookstats", bookService.searchBookstats(pager,code));
		data.put("pager", pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.havaBook", data);
		return JSONUtil.toJSON(result);
	}
	
	//TODO 取消预约 修改状态及取消预约时间
	/**
	 * 取消预约
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("reserveCancell")
	public Object reservCancell(Integer id)  throws Exception{
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.cancellSuccess", data);
		if (!bookService.editReserveCancell(id)) {
			result = new Result(SysConstant.STATE_ERROR, "books.cancellError", data);
		}
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 还书
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("returnedBook")
	public Object returnedBook(Integer id)  throws Exception{
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.returnedSuccess", data);
		if (!bookService.editReturnedBook(id)) {
			result = new Result(SysConstant.STATE_ERROR, "books.returnedError", data);
		}
		return JSONUtil.toJSON(result);
	}
	
	/**
	 * 取书
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("takeBook")
	public Object takeBook(Integer id)  throws Exception{
		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "books.takeSuccess", data);
		if (!bookService.editTakeBook(id)) {
			result = new Result(SysConstant.STATE_ERROR, "books.takeError", data);
		}
		return JSONUtil.toJSON(result);
	}
}
